<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class VoteController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->vote = D("Vote");
		$this->question = D("VoteQuestion");
		$this->answer = D("VoteAnswer");
		$this->log = D("VoteLog");
		$this->types=array("select"=>"单选","selects"=>"多选","input"=>"单行文本","textarea"=>"多行文本");
		
	}
	
	function index(){
	


		$count=$this->vote->count();
		$page = $this->page($count, 20);
		$list=$this->vote->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
		$this->assign("list",$list);
		$this->assign("Page",$page->show(""));

		
		
		$this->display();
	}


	function  answer_list(){



		$id= intval(I("get.id"));
		$cate=$this->question->where("id=$id")->find();


		$list= $this->answer->where(array("question_id"=>$id))->order("listorder desc ,id asc")->select();
		$this->assign("data",$cate);
		$this->assign("list",$list);





		$this->display();

	}

	function answer_add(){


		if ($this->answer->create($_POST)) {
			$id=$this->answer->add();
			if ($id) {
				$this->success("添加成功！");
			} else {
				$this->error("添加失败！");
			}
		} else {
			$this->error($this->answer->getError());
		}


	}


	function question_add(){

		if (IS_POST) {






			if ($this->question->create($_POST)) {
				$id=$this->question->add();
				if ($id) {
					$this->success("添加成功！");
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->question->getError());
			}
		}else{

			$this->display();
		}
	}




	function add(){

		if (IS_POST) {



			if ($this->vote->create($_POST)) {
				$id=$this->vote->add();
				if ($id) {
					$this->success("添加成功！", U("vote_show",array("id"=>$id)));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->vote->getError());
			}
		}else{

			$this->display();
		}
		}

	function  vote_show(){

		$vote_id=I("get.id");
		if(!$vote_id){
			$this->error("缺少参数！");
		}
		$list=$this->question->where(array("vote_id"=>$vote_id))->order(array("listorder" => "DESC","id"=>"asc"))->select();

		$vote=$this->vote->where(array("id"=>$vote_id))->find();



		$this->assign("vote",$vote);
		$this->assign("list",$list);
		$this->display();

	}

	function  save(){




		$list=I('post.questions');


		foreach($list as $k=>$v){


			$this->question->where(array("id"=>$k))->save($v);


		}
		$this->success("保存成功！");




	}

	function  answer_save(){




		$list=I('post.answers');


		foreach($list as $k=>$v){


			$this->answer->where(array("id"=>$k))->save($v);


		}
		$this->success("保存成功！");




	}
	
	
	function edit(){
		
		
		if(IS_POST){
			if ($this->vote->create()) {
				
				if ($this->vote->save()!==false) {
					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->vote->getError());
			}
		
		}else{

			$id = intval(I("get.id"));
			if (!$id) {
				$this->error("缺少参数！");
			}


			$data = $this->vote->where("id=$id")->find();



			$this->assign("data",$data);
		$this->display();
		}
	}

	/**
	 * 状态转换
	 */
	public function changestatus() {
		$id = $_REQUEST[id];
		$r = $this->vote->where(array("id" => $id))->find();

		if ($r) {
			$data[status] = $r['status'] ? 0 : 1;
			$data[id]=$id;
			$result=  $this->vote->save($data);
			if ($result !== false) {

				$this->success("转换成功！");

			} else {
				$this->error("转换失败！");
			}
		} else {
			$this->error("不存在！");
		}
	}


	/**
	 * 状态转换
	 */
	public function changestatus_question() {
		$id = $_REQUEST[id];
		$r = $this->question->where(array("id" => $id))->find();

		if ($r) {
			$data[required] = $r['required'] ? 0 : 1;
			$data[id]=$id;
			$result=  $this->question->save($data);
			if ($result !== false) {

				$this->success("转换成功！");

			} else {
				$this->error("转换失败！");
			}
		} else {
			$this->error("不存在！");
		}
	}


	function result(){
		$id=I("get.id");


		$info=$this->vote->find($id);


		$order="listorder desc, id asc";
		$question=$this->question->where(array("vote_id"=>$id))->order($order)->select();

		$sums=$this->log->where(array("vote_id"=>$id))->group("uid")->field('uid')->count();
		$sum=count($sums);
		$this->assign("sum",$sum);

		foreach($question as $k=>$v){

			$s=0;


			if($v['type']=='select'|| $v['type']=='selects'){



				$answer=$this->answer->order($order)->where(array("question_id"=>$v['id']))->select();


				foreach($answer as $vv){

					$a_count=$this->log->where(array("question_id"=>$v['id'],"value"=>$vv['id']))->count();


					$vv["bili"]=intval($a_count/$sum*100);
					$vv["count"]=$a_count;


					$question[$k]['answer'][]=$vv;

					$s=$s+$a_count;

				}

				if(!$v["required"]&&$v['type']=='select'){

					$question[$k]['answer'][]=array(
						"id"=>"",
						"value"=>"未选择",
						"bili"=>intval(($sum-$s)/$sum*100),
						"count"=>$sum-$s
					);



				}

			}

		}

		$this->assign("list",$question);

		$this->display();



	}


	function  log(){

		$vote_id=I('get.vote_id');
		$question_id=I('get.question_id');
		if($vote_id){
			$map["vote_id"]=$vote_id;
		}if($question_id){
			$map["question_id"]=$question_id;
		}



		$vote_id=$this->question->where(array("id"=>I('get.id')))->getField("vote_id");


		$count=$this->log->where($map)->count();
		$page = $this->page($count);
		$logs=$this->log->where($map)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();


		foreach($logs as $k=>$v){



			$question=$this->question->where(array("id"=>$v["question_id"]))->find();
			$logs[$k]["question"]=$question['title'];

			if($question['type']=="select"||$question['type']=="selects")
			{
				$logs[$k]["value"]=$this->answer->where(array("id"=>$v["value"]))->getField("value");

			}




		}

		$this->assign("Page",$page->show(""));

		$this->assign("list",$logs);
		$this->assign("vote_id",$vote_id);



		$this->display();
	}

	function delete(){
		
			$id = intval(I("get.id"));



		$questions=$this->question->where(array("vote_id"=>$id))->getField("id",true);

		$this->answer->where(array("question_id"=>array("in",$questions)))->delete();
		$this->log->where(array("vote_id"=>$id))->delete();
		$this->question->where(array("vote_id"=>$id))->delete();

		$this->vote->delete($id);


		$this->success("删除成功！");


	}
	

	function question_del(){

		$id = intval(I("get.id"));

		if(!$id){
			$this->error("缺少参数！");
		}

		$this->question->delete($id);

		$this->answer->where(array("question_id"=>$id))->delete();
		$this->log->where(array("question_id"=>$id))->delete();

		$this->success("删除成功！");



	}
	
	

	
	//排序


	}